package lcr;

public class Test0143 {
    public static void main(String[] args)
    {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(0);
        TreeNode node3 = new TreeNode(1);
        TreeNode node4 = new TreeNode(-4);
        TreeNode node5 = new TreeNode(-3);
        TreeNode node6 = new TreeNode(1);
        TreeNode node7 = new TreeNode(-4);
        TreeNode node8 = new TreeNode(8);
        TreeNode node9 = new TreeNode(9);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;

        node6.left = node7;

        boolean b = new Solution0143_1().isSubStructure(node1, node6);
        System.out.println(b);
    }
}

class Solution0143_1 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B==null||A==null) return false;
        return dfs(A,B,false);
    }
    public boolean dfs(TreeNode A, TreeNode B,boolean flag){
        if (B==null) return true;
        if (A==null) return false;

        if (A.val==B.val&&dfs(A.left,B.left,true)&&dfs(A.right,B.right,true)){
            return true;
        }

        return !flag && (dfs(A.left, B, flag) || dfs(A.right, B, flag));

    }
}